package tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.api.VideoApi;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import tanhua.dubbo.server.enums.IdType;
import tanhua.dubbo.server.service.IdService;

/**
 * @program: my-tanhua
 * @description:
 * @author: xuejingjing
 * @create: 2021-10-04 18:42
 **/

@Service(version = "1.0.0")
@Slf4j
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;

    @Autowired
    private RedisTemplate<Object,String> redisTemplate;
    /**
     * 发布小视频
     * @param video
     * @return 保存小视频后返回小视频的id
     */
    @Override
    public String saveVideo(Video video) {
        try {
            //判断传入的video的属性不能为空
            if(!(ObjectUtil.isAllNotEmpty(video.getPicUrl(),video.getUserId(),video.getVideoUrl())) {
                return null;
            }
            video.setId(ObjectId.get());
            video.setVid(idService.created(IdType.VIDEO));
            //发布时间
            video.setCreated(System.currentTimeMillis());
            mongoTemplate.save(video);
            //返回video的id
            return  video.getId().toHexString();
        } catch (Exception e) {
            log.error("小视频发布失败,video = "+video,e);
        }
         return null;
    }

    /**
     * 分页查询小视频列表
     * 优先展现推荐的视频，如果没有推荐的视频或者已经查询完成，就需要查询系统视频数据。
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Video> queryVideoList(Long userId, Integer page, Integer pageSize) {
        PageInfo<Video> pageInfo = new PageInfo<>();
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        //从redis中获取推荐视频的数据
        String redisKey = "QUANZI_VIDEO_RECOMMEND_" + userId;
        String redisData = redisTemplate.opsForValue()


        return null;
    }
}